﻿using ArtMath.Attribute;
using ArtMath.Component;
using ArtMath.Component.Vector;
using ArtMath.Core.Helper;
using System;

namespace ArtMath.Parameter
{
    /// <summary>
    /// 两向量数量积
    /// </summary>
    [Serializable]
    public class TwoVectorsScalarProductParameter : CalculatedParameter
    {
        VectorBase v1, v2;
        public TwoVectorsScalarProductParameter(SketchPad pad, string name, VectorBase v1, VectorBase v2) : base(pad, name, ToolTypeName.lxldslj)
        {
            Vector1 = v1;
            Vector2 = v2;
        }
        [MultilingualCategory("Structure"), MultilingualDisplayName("{0}1", "Vector")]
        public VectorBase Vector1
        {
            get { return v1; }
            set
            {
                if (v1 is object)
                {
                    v1.CalculatedParameters.Remove(this);
                }
                v1 = value;
                if (v1 is object)
                {
                    v1.CalculatedParameters.Add(this);
                    Value.SetExpression(GetNewValue());
                }
            }
        }
        [MultilingualCategory("Structure"), MultilingualDisplayName("{0}2", "Vector")]
        public VectorBase Vector2
        {
            get { return v2; }
            set
            {
                if (v2 is object)
                {
                    v2.CalculatedParameters.Remove(this);
                }
                v2 = value;
                if (v2 is object)
                {
                    v2.CalculatedParameters.Add(this);
                    Value.SetExpression(GetNewValue());
                }
            }
        }
        protected override void ForEachRelatedObject(Predicate<Base> action)
        {
            if (action == null) return;
            if (!action(Vector1)) return;
            if (!action(Vector2)) return;
        }
        public override double GetNewValue()
        {
            if (Vector1 is null || Vector2 is null)
            {
                return 0;
            }
            return Utility.ScalarProduct(Vector1.Point1.NewXY, Vector1.Point2.NewXY, Vector2.Point1.NewXY, Vector2.Point2.NewXY);
        }
        public override void Delete(bool recoverable = false)
        {
            base.Delete(recoverable);
            if (recoverable)
            {

            }
            else
            {
                Vector1 = null;
                Vector2 = null;
            }
        }
    }
}
